﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany
{
    public partial class account_ctrl : UserControl
    {
        private eCompany.eObj.company m_company = null;
        private eCompany.eObj.organisation m_org = null;
        private eCompany.eObj.account m_acc = null;
        private eCompany.eObj.currency_list m_cl = null;

        public account_ctrl(eCompany.eObj.company company, eCompany.eObj.account acc, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            m_company = company;
            m_acc = acc;
            m_cl = cl;
            RefreshTransactionList();
            InitGraph();
        }

        public account_ctrl(eCompany.eObj.organisation organisation, eCompany.eObj.account acc, eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            m_org = organisation;
            m_acc = acc;
            m_cl = cl;
            RefreshTransactionList();
            InitGraph();
        }
        
        private void RefreshTransactionList()
        {
            double before_solde = 0;
            double before_profit = 0;
            double yesterday_solde = 0;
            double yesterday_profit = 0;
            double today_solde = 0;
            double today_profit = 0;
            eCompany.eManager.eManager eM = new eManager.eManager();
            
            ListViewGroup today_grp = new ListViewGroup();
            today_grp.Header = "Today";
            transactionListView.Groups.Add(today_grp);

            ListViewGroup yesterday_grp = new ListViewGroup();
            yesterday_grp.Header = "Yesterday";
            transactionListView.Groups.Add(yesterday_grp);

            ListViewGroup before_grp = new ListViewGroup();
            before_grp.Header = "Before";
            transactionListView.Groups.Add(before_grp);

            transactionListView.Items.Clear();

            foreach (eCompany.eObj.transaction t in m_acc.transaction_list)
            {
                ListViewItem item = new ListViewItem();
                item.Tag = t.id;
                item.Text = t.Date.ToString();
                item.SubItems.Add(t.Type.ToString());
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(t.Value)+m_acc.currency);
                item.SubItems.Add(eCompany.eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl, m_acc.currency, t.Value)) + "G");
                if (t.Type == eObj.transaction_type.Salary)
                {
                    item.SubItems.Add(eM.getEmployee(m_company, Guid.Parse(t.info)).name);
                }
                if (t.Date.Date == DateTime.Today)
                {
                    today_solde += t.Value;
                    today_profit += t.Value;
                    item.Group = today_grp;
                }
                else if (t.Date.Date == DateTime.Today.AddDays(-1))
                {
                    today_solde += t.Value;
                    yesterday_solde += t.Value;
                    yesterday_profit += t.Value;
                    item.Group = yesterday_grp;
                }
                else
                {
                    today_solde += t.Value;
                    before_solde += t.Value;
                    yesterday_solde += t.Value;
                    before_profit += t.Value;
                    item.Group = before_grp;
                }
                transactionListView.Items.Add(item);
            }
            today_grp.Header += "    (Solde=" + eCompany.eManager.eManager.formatDouble(today_solde)+m_acc.currency+"/"+eCompany.eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,today_solde))+"G    Profit="+eCompany.eManager.eManager.formatDouble(today_profit)+m_acc.currency+"/"+eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,today_profit))+"G)";
            yesterday_grp.Header += "    (Solde=" + eCompany.eManager.eManager.formatDouble(yesterday_solde)+m_acc.currency+"/"+eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,yesterday_solde)) + "G    Profit=" + eCompany.eManager.eManager.formatDouble(yesterday_profit) +m_acc.currency+"/"+eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,yesterday_profit))+ "G)";
            before_grp.Header += "    (Solde=" + eCompany.eManager.eManager.formatDouble(before_solde) +m_acc.currency+"/"+eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,before_solde))+ "G    Profit=" + eCompany.eManager.eManager.formatDouble(before_profit) +m_acc.currency+"/"+eManager.eManager.formatDouble(eM.currencyToGOLD(m_cl,m_acc.currency,before_profit))+ "G)";
            transactionListView.SetGroupState(ListViewGroupState.Collapsible | ListViewGroupState.Collapsed);
        }

        private void account_ctrl_Load(object sender, EventArgs e)
        {
            if(transactionListView.Items.Count>0)
                transactionListView.EnsureVisible(transactionListView.Items.Count - 1);
        }

        private void InitGraph()
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            List<eCompany.eManager.ChartItem> lst = eM.getAccountChartValue(m_acc, m_cl);

            System.Windows.Forms.DataVisualization.Charting.Series serie = accountChart.Series[0];
            foreach (eCompany.eManager.ChartItem item in lst)
            {
                serie.Points.AddXY(item.date, item.volume);
            }
        }

        private void adjustAccountValueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.eManager.eManager eM = new eManager.eManager();

            string str_new_value = "";

            if (Tools.InputBox("Adjust account", "New value", ref str_new_value) == DialogResult.OK)
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                str_new_value = str_new_value.Replace(",", ".");
                double new_value = Convert.ToDouble(str_new_value, culture);
                eM.adjustAccount(m_acc, new_value);
                RefreshTransactionList();
            }
        }

        private void transfertMoneyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            eCompany.Forms.transfert_form f = null;

            if(m_company != null)
                f = new eCompany.Forms.transfert_form(m_company, m_acc, m_cl);
            else
                f = new eCompany.Forms.transfert_form(m_org, m_acc, m_cl);
            f.Show();
            RefreshTransactionList();
        }
    }
}
